package com.bestcem.xm.ticket.controller.param.ticket.config.createParam;

import com.bestcem.xm.ticket.constant.ColumnConstant;
import com.bestcem.xm.ticket.entity.mongo.TicketFlow;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.util.CollectionUtils;

import javax.annotation.Nullable;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @author zq.ding <zq.ding@idiaoyan.com>
 * @version v1.0
 * @date 2021/9/8 16:59
 */
@NoArgsConstructor
@AllArgsConstructor
@Data
public class FollowConfigParam implements Serializable {

    private static final long serialVersionUID = 5354227783048186834L;
    /**
     * title
     */
    @JsonProperty(value = ColumnConstant.TicketFlow.FollowConfig.TITLE)
    private String title;

    /**
     * follower_email
     */

    @JsonProperty(value = "follower_email")
    private String followerEmail;

    /**
     * follower_id
     */

    @JsonProperty(value = "follower_id")
    private String followerId;

    /**
     * follower_name
     */

    @JsonProperty(value = "follower_name")
    private String followerName;

    /**
     * follower_filter
     */

    @JsonProperty(value = "follower_filter")
    private Integer followerFilter;

    /**
     * region
     */
    @JsonProperty(value = ColumnConstant.TicketFlow.FollowConfig.REGION)
    private String region;

    /**
     * roleId
     */
    @JsonProperty(value = ColumnConstant.TicketFlow.FollowConfig.ROLE_ID)
    private String roleId;

    /**
     * timeLimitType
     */
    @JsonProperty(value = ColumnConstant.TicketFlow.FollowConfig.TIME_LIMIT_TYPE)
    private Integer timeLimitType;

    /**
     * timeLimit
     */
    @JsonProperty(value = ColumnConstant.TicketFlow.FollowConfig.TIME_LIMIT)
    private String timeLimit;

    /**
     * overdueHandle
     */
    @JsonProperty(value = ColumnConstant.TicketFlow.FollowConfig.OVERDUE_HANDLE)
    private Integer overdueHandle;

    /**
     * emptyFollowerHandle
     */
    @JsonProperty(value = ColumnConstant.TicketFlow.FollowConfig.EMPTY_FOLLOWER_HANDLE)
    private Integer emptyFollowerHandle;

    /**
     * options
     */
    @JsonProperty(value = ColumnConstant.TicketFlow.FollowConfig.OPTIONS)
    private List<OptionParam> options;

    @Nullable
    public static TicketFlow.FollowConfig paramToEntity(FollowConfigParam param) {
        if (param == null) {
            return null;
        }
        TicketFlow.FollowConfig followConfig = new TicketFlow.FollowConfig();
        followConfig.setTitle(param.getTitle());
        followConfig.setFollowerEmail(param.getFollowerEmail());
        followConfig.setFollowerId(param.getFollowerId());
        followConfig.setFollowerName(param.getFollowerName());
        followConfig.setFollowerFilter(param.getFollowerFilter());
        followConfig.setRegion(param.getRegion());
        followConfig.setRoleId(param.getRoleId());
        followConfig.setTimeLimitType(param.getTimeLimitType());
        followConfig.setTimeLimit(param.getTimeLimit());
        followConfig.setOverdueHandle(param.getOverdueHandle());
        followConfig.setEmptyFollowerHandle(param.getEmptyFollowerHandle());
        List<TicketFlow.FollowConfig.Option> optionList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(param.getOptions())) {
            List<OptionParam> options = param.getOptions();
            for (OptionParam option : options) {
                TicketFlow.FollowConfig.Option temp = OptionParam.paramToEntity(option);
                if (Objects.nonNull(temp)) {
                    optionList.add(temp);
                }
            }
        }
        followConfig.setOptions(optionList);
        return followConfig;
    }
}